package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.Brand;
import com.sailfish.springbootdemo.pojo.db1.BrandType;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface BrandTypeDao {

    /**
     * 查询所有
     *
     * @param searchValue
     * @return
     */
    @Results(id = "brandTypeResultMap", value = {
            @Result(column = "brand_type_id", property = "brandTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "brand_id", property = "brandId", jdbcType = JdbcType.INTEGER),
            @Result(column = "brand_type_name", property = "brandTypeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.BRAND_TYPE + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and brand_type_name like CONCAT('%',#{searchValue},'%')" +
            "</if>" +
            "<if test='brandId != null and brandId != \"\" '>" +
            " and brand_id = #{brandId}" +
            "</if>" +
            " order by sort " +
            "</script>")
    List<BrandType> getAllBrandType(@Param("searchValue") String searchValue, @Param("brandId") Integer brandId);

    /**
     * @param brandTypeName
     * @return
     */
    @ResultMap(value = "brandTypeResultMap")
    @Select("select * from " + DB1TableName.BRAND_TYPE + " where brand_type_name = #{brandTypeName} and brand_id = #{brandId}")
    List<BrandType> getBrandTypeByName(@Param("brandTypeName") String brandTypeName, @Param("brandId") Integer brandId);

    /**
     * 根据id查询
     *
     * @param brandTypeId
     * @return
     */
    @ResultMap(value = "brandTypeResultMap")
    @Select("select * from " + DB1TableName.BRAND_TYPE + " where brand_type_id = #{brandTypeId}")
    BrandType getBrandTypeById(@Param("brandTypeId") Integer brandTypeId);

    @ResultMap(value = "brandTypeResultMap")
    @Select("select * from " + DB1TableName.BRAND_TYPE + " where brand_type_id = #{brandTypeId} and brand_id = #{brandId}")
    BrandType getBrandTypeByIdAndBrand(@Param("brandTypeId") Integer brandTypeId, @Param("brandId") Integer brandId);

    /**
     * 判断有没有重复的
     *
     * @return
     */
    @ResultMap(value = "brandTypeResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.BRAND_TYPE + " where brand_type_name = #{brandTypeName} and brand_id = #{brandId} " +
            "<if test='brandTypeId != null and brandTypeId != \"\" '>" +
            " and brand_type_id != #{brandTypeId}" +
            "</if>" +
            "</script>")
    List<BrandType> judgeUniqueBrandType(@Param("brandTypeId") Integer brandTypeId, @Param("brandId") Integer brandId, @Param("brandTypeName") String brandTypeName);

    /**
     * 根据品牌+型号id查询
     *
     * @param brandTypeId
     * @param brandId
     * @return
     */
    @ResultMap(value = "brandTypeResultMap")
    @Select("select * from " + DB1TableName.BRAND_TYPE + " where brand_type_id = #{brandTypeId} and brand_id = #{brandId}")
    BrandType getBrandTypeByBrandAndTypeId(@Param("brandTypeId") Integer brandTypeId, @Param("brandId") Integer brandId);

    /**
     * 删除
     *
     * @param brandTypeId
     * @return
     */
    @Delete("delete from " + DB1TableName.BRAND_TYPE + " where brand_type_id = #{brandTypeId}")
    Integer deleteBrandTypeById(@Param("brandTypeId") Integer brandTypeId);

    /**
     * 新增
     *
     * @param brandType
     * @return
     */
    @Insert("insert into " + DB1TableName.BRAND_TYPE + "(brand_type_name,status,sort,create_time,brand_id) values " +
            "(#{brandType.brandTypeName},#{brandType.status},#{brandType.sort},#{brandType.createTime},#{brandType.brandId})")
    Integer addBrandType(@Param("brandType") BrandType brandType);

    /**
     * 更新
     *
     * @param brandType
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.BRAND_TYPE +
            "<set>" +
            "<if test='brandType.brandTypeName != null and brandType.brandTypeName != \"\" '>" +
            " brand_type_name = #{brandType.brandTypeName}," +
            "</if>" +
            "<if test='brandType.status != null and brandType.status != \"\" '>" +
            " status = #{brandType.status}," +
            "</if>" +
            "<if test='brandType.sort != null and brandType.sort != \"\" '>" +
            " sort = #{brandType.sort}," +
            "</if>" +
            "<if test='brandType.brandId != null and brandType.brandId != \"\" '>" +
            " brand_id = #{brandType.brandId}," +
            "</if>" +
            "<if test='brandType.updateTime != null '>" +
            " update_time = #{brandType.updateTime}," +
            "</if>" +
            "</set>" +
            " where brand_type_id = #{brandType.brandTypeId}" +
            "</script>")
    Integer updateBrandType(@Param("brandType") BrandType brandType);
}
